package downloader

import (
	"net/http"

	"MyWebcrawler/module"
	"MyWebcrawler/module/modelInternal"
	"github.com/sirupsen/logrus"
)

// New 用于创建一个下载器实例。
func New(mid module.MID, client *http.Client, CalculatorFunc module.CalculateScore) (module.Downloader, error) {
	moduleBase, err := modelInternal.NewModuleInternal(mid, CalculatorFunc)
	if err != nil {
		return nil, err
	}
	if client == nil {
		return nil, genDownloaderErr("nil http client")
	}
	return &myDownloader{
		ModuleInternal: moduleBase,
		httpClient:     *client,
	}, nil
}

// myDownloader 代表下载器的实现类型。
type myDownloader struct {
	// stub.ModuleInternal 代表组件基础实例。
	modelInternal.ModuleInternal
	// httpClient 代表下载用的HTTP客户端。
	httpClient http.Client
}

func (downloader *myDownloader) Download(req *module.Request) (*module.Response, error) {
	downloader.ModuleInternal.IncrHandlingNumber()
	defer downloader.ModuleInternal.DecrHandlingNumber()
	downloader.ModuleInternal.IncrCalledCount()
	if req == nil {
		return nil, genDownloaderErr("nil request")
	}
	httpReq := req.HTTPReq()
	if httpReq == nil {
		return nil, genDownloaderErr("nil HTTP request")
	}
	downloader.ModuleInternal.IncrAcceptedCount()
	logrus.Infof("Do the request (URL: %s, depth: %d)... \n", httpReq.URL, req.Depth())
	httpResp, err := downloader.httpClient.Do(httpReq)
	if err != nil {
		return nil, err
	}
	downloader.ModuleInternal.IncrCompletedCount()
	return module.NewResponse(httpResp, req.Depth()), nil
}
